package com.lh.service.impl.center;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lh.enums.OrderStatusEnum;
import com.lh.enums.YesOrNo;
import com.lh.mapper.OrderStatusExtMapper;
import com.lh.mapper.OrderStatusMapper;
import com.lh.mapper.OrdersExtMapper;
import com.lh.mapper.OrdersMapper;
import com.lh.pojo.OrderStatus;
import com.lh.pojo.Orders;
import com.lh.pojo.vo.MyOrdersVO;
import com.lh.pojo.vo.OrderStatusCountsVO;
import com.lh.service.center.MyOrderService;
import com.lh.utils.PagedGridResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: foodie-dev
 * @description:
 * @author: LH
 * @create: 2022-05-26 10:01
 **/
@Service
public class MyOrderServiceImpl implements MyOrderService {

    @Autowired
    private OrdersExtMapper ordersExtMapper;
    @Autowired
    private OrderStatusMapper orderStatusMapper;
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private OrderStatusExtMapper orderStatusExtMapper;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public PagedGridResult queryMyOrdersVO(String userId, Integer orderStatus, Integer page, Integer pageSize) {

        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        if (orderStatus != null){
            map.put("orderStatus", orderStatus);
        }

        PageHelper.startPage(page, pageSize);

        List<MyOrdersVO> myOrdersVOList = ordersExtMapper.queryMyOrder(map);

        PagedGridResult pageData = getPageData(myOrdersVOList, page);

        return pageData;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public PagedGridResult getPageData(List<?> list, Integer page){
        PageInfo<?> pageList = new PageInfo<>(list);
        PagedGridResult grid = new PagedGridResult();
        grid.setPage(page);
        grid.setRows(list);
        grid.setTotal(pageList.getPages());
        grid.setRecords(pageList.getTotal());
        return grid;
    }


    @Override
    @Transactional(propagation=Propagation.REQUIRED)
    public void updateDeliverOrderStatus(String orderId) {

        OrderStatus updateOrder = new OrderStatus();
        updateOrder.setOrderStatus(OrderStatusEnum.WAIT_RECEIVE.getType());
        updateOrder.setDeliverTime(new Date());

        Example example = new Example(OrderStatus.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("orderId", orderId);
        criteria.andEqualTo("orderStatus", OrderStatusEnum.WAIT_DELIVER.getType());

        orderStatusMapper.updateByExampleSelective(updateOrder, example);
    }

    @Override
    @Transactional(propagation=Propagation.SUPPORTS)
    public Orders queryMyOrder(String userId, String orderId) {

        Orders orders = new Orders();
        orders.setUserId(userId);
        orders.setId(orderId);
        orders.setIsDelete(YesOrNo.NO.getType());

        Orders ordersResult = ordersMapper.selectOne(orders);

        return ordersResult;
    }

    @Override
    @Transactional(propagation=Propagation.REQUIRED)
    public boolean confirmReceive(String orderId) {

        OrderStatus orderStatus = new OrderStatus();
        orderStatus.setOrderStatus(OrderStatusEnum.SUCCESS.getType());
        orderStatus.setSuccessTime(new Date());

        Example example = new Example(OrderStatus.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("orderId", orderId);
        criteria.andEqualTo("orderStatus", OrderStatusEnum.WAIT_RECEIVE.getType());

        int i = orderStatusMapper.updateByExampleSelective(orderStatus, example);

        return i == 1 ? true : false;
    }

    @Override
    @Transactional(propagation=Propagation.REQUIRED)
    public boolean delete(String userId, String orderId) {
        Orders orders = new Orders();
        orders.setIsDelete(YesOrNo.YES.getType());
        orders.setUpdatedTime(new Date());

        Example example = new Example(Orders.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", orderId);
        criteria.andEqualTo("userId", userId);
        criteria.andEqualTo("isDelete", YesOrNo.NO.getType());

        int i = ordersMapper.updateByExampleSelective(orders, example);

        return i == 1 ? true : false;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public OrderStatusCountsVO statusCounts(String userId) {

        OrderStatusCountsVO result = new OrderStatusCountsVO();

        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);

        map.put("orderStatus", OrderStatusEnum.WAIT_PAY.getType());
        Integer payCounts = orderStatusExtMapper.statusCounts(map);
        result.setWaitPayCounts(payCounts);

        map.put("orderStatus", OrderStatusEnum.WAIT_DELIVER.getType());
        Integer deliverCounts = orderStatusExtMapper.statusCounts(map);
        result.setWaitDeliverCounts(deliverCounts);

        map.put("orderStatus", OrderStatusEnum.WAIT_RECEIVE.getType());
        Integer receiveCounts = orderStatusExtMapper.statusCounts(map);
        result.setWaitReceiveCounts(receiveCounts);

        map.put("orderStatus", OrderStatusEnum.SUCCESS.getType());
        map.put("isComment", YesOrNo.NO.getType());
        Integer commentCounts = orderStatusExtMapper.statusCounts(map);
        result.setWaitCommentCounts(commentCounts);

        return result;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public PagedGridResult trend(String userId, Integer page, Integer pageSize) {

        PageHelper.startPage(page, pageSize);
        List<OrderStatus> statusList = orderStatusExtMapper.trend(userId);

        PagedGridResult pageData = getPageData(statusList, page);
        return pageData;
    }

}
